package com.fei.hunqing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fei.hunqing.dao.UserDao;
import com.fei.hunqing.dao.UserDetailDao;
import com.fei.hunqing.dao.UserGoldDao;
import com.fei.hunqing.dto.UserAddDto;
import com.fei.hunqing.dto.UserFindDto;
import com.fei.hunqing.dto.UserLoginDto;
import com.fei.hunqing.entity.User;
import com.fei.hunqing.entity.UserDetail;
import com.fei.hunqing.entity.UserGold;
import com.fei.hunqing.service.intf.UserService;
import com.fei.hunqing.util.JedisUtil;
import com.fei.hunqing.util.PassUtil;
import com.fei.hunqing.vo.R;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.Date;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao dao;
    @Resource
    private UserDetailDao detailDao;
    @Resource
    private UserGoldDao goldDao;

    private final String PASS_KEY="PvDCLxg7IkwBr/idSiOWcA==";
    @Override
    public R checkPhone(String phone) {
        //1.校验手机号是否为空
        if (StringUtils.hasLength(phone)){
            //2.手机号 正则校验 是否符合手机号格式
            if(phone.matches("^1[3456789]\\d{9}$")){
                //3.查询数据库完成校验
                if (dao.selectOne(new QueryWrapper<User>().eq("phone",phone))==null){
                    return R.ok();
                }
            }
        }
        return R.fail("亲,此手机号已被注册!");
    }

    @Override
    public R checkName(String name) {
        //1.校验昵称是否为空
        if (StringUtils.hasLength(name)){
            //2.查询数据库完成校验
            if (dao.selectOne(new QueryWrapper<User>().eq("name",name))==null){
                return R.ok();
            }

        }
        return R.fail("亲,此昵称已被注册!");
    }

    @Transactional(rollbackFor = Exception.class) //开启事务
    @Override
    public R register(UserAddDto dto) {
        //1.检验是否为空
        if (dto!=null){
            if (StringUtils.hasLength(dto.getPhone()) && StringUtils.hasLength(dto.getName())){
                //2.校验手机号或昵称是否存在
                QueryWrapper<User> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("phone",dto.getPhone()).or().eq("name",dto.getName());
                if (dao.selectOne(queryWrapper)==null){
                    User user = new User();
                    //3.密码加密
                    user.setPassword(PassUtil.aesEnc(PASS_KEY,dto.getPassword()));
                    user.setName(dto.getName());
                    user.setPhone(dto.getPhone());
                    user.setFlag(1);
                    user.setCtime(new Date());
                    //4.校验该手机号 是否成功验证过 验证码
                    if(JedisUtil.existsSet("auth-checkphones",dto.getPhone())) {
                        //5.操作数据库
                        if (dao.insert(user) > 0) {
                            //6.注册成功,删除掉，原来记录的验证成功的手机号
                            JedisUtil.delSet("auth-checkphones",dto.getPhone());
                            //7.完成新人有礼  用户详情的初始化、坤币奖励
                            detailDao.insert(new UserDetail(user.getId(),100));
                            //8.记录 坤币变动
                            goldDao.insert(new UserGold(user.getId(), 1,100,"新用户注册奖励100坤币！"));
                            return R.ok();
                        }
                    }
                }
            }
        }
        return R.fail("亲,注册失败！");
    }

    @Override
    public R loginAccount(UserLoginDto dto) {
        return null;
    }

    @Override
    public R loginCode(UserLoginDto dto) {
        return null;
    }

    @Override
    public R findPassword(UserFindDto dto) {
        return null;
    }

    @Override
    public R loginOut(String phone) {
        return null;
    }

    @Override
    public R checkToken(String phone, String token) {
        return null;
    }

    @Override
    public R all() {
        return null;
    }
}
